<?php
namespace app\admin\controller;
use app\admin\common\Base;
use think\Db;
use think\Session;

use app\admin\model\CodeRule as CodeRuleModel;

class Coderule extends Base
{
    public function index()
    {
    	$data=$this->request->param();

    	if (isset($data['ajax']) && $data['ajax'] == 100) {
    		$map='';
    		$page=isset($data['page'])?$data['page']:1;
            $page=intval($page);
            $limit=isset($data['rows'])?$data['rows']:10;
            $limit=intval($limit);
            $start=$limit*($page-1);

    	
    		if($map==''){
                $list =Db::name('code_rule')->where('is_del',0)->where('enterprise_id',$this->EnterpriseId)->order('id asc')->limit($start,$limit)->select();
                $count =Db::name('code_rule')->where('enterprise_id',$this->EnterpriseId)->where('is_del',0)->count();
            }else{
                $list =Db::name('code_rule')->where('is_del',0)->order('id asc')->where('enterprise_id',$this->EnterpriseId)->limit($start,$limit)->select();
                $count =Db::name('code_rule')->where('enterprise_id',$this->EnterpriseId)->where('is_del',0)->count();
            }

            foreach ($list as $key => &$value) {
                $value['type'] = Db::name('rule_type')->where('id',$value['type'])->value('name');
                if($value['is_default'] == 0){
                    $value['is_default'] = '否';
                }else{
                    $value['is_default'] = '是';
                }
            }
            return $this->grid_data($list,$count,$page,$limit); 
    	}
    	return $this->fetch();
    }

    public function add()
    {
        $type = Db::name('rule_type')->where('is_del',0)->select();
        $this->assign('type',$type);
        return $this->fetch();
    }

    //编辑栏目1-1
    public function edit($id)
    {
        $info = Db::name('code_rule')->where('id', $id)->find();
        $type = Db::name('rule_type')->where('is_del',0)->select();
        $this->assign('type',$type);
        $this->assign('info', $info);
        return $this->fetch();
    }
    //编辑栏目1-2
    public function save()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();
        $data['Y'] = intval($data['Y']??0);
        $data['M'] = intval($data['M']??0);
        $data['D'] = intval($data['D']??0);
        $data['is_default'] = intval($data['is_default']??0);
        // 改变所有默认为否
        if($data['is_default'] == 1){
            Db::name('code_rule')->where(['type'=>$data['type'], 'enterprise_id'=>$this->EnterpriseId])->update(['is_default'=>0]);
        }
        $model = new CodeRuleModel();
        if (empty($data['id'])) {
            $data['enterprise_id'] = $this->EnterpriseId;
            $result = $model->allowField(true)->save($data);
            if ($result) {
                $status = 1;
                $msg = "添加成功";
            }
        } else {
            $id = $data['id'];
            $result = $model->allowField(true)->save($data,["id"=>$id]);
            if ($result) {
                $status = 1;
                $msg = "更新成功";
            }
        }
        return ['status' => $status, 'msg' => $msg];
    }

    //删除栏目1-1
    public function del($id)
    {
        $status = 0;
        $msg = "操作失败";
        $del = Db::name('code_rule')->where('id', $id)->update(["is_del"=>1]);
        if ($del) {
            $status = 1;
            $msg = "删除成功";
        }
        return ['status' => $status, 'msg' => $msg];
    }


}
